﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace FileSharing
{
    public class Settings
    {
        private Dictionary<Setting, string> variables = new Dictionary<Setting, string>();

        private static Settings singleton;

        public enum Setting
        {
            DefaultAddress,
            DefaultPort,
            IncomingPort,
            RandomizeIncomingPort,
            IncomingDirectory,
            UserName,
            KnownUsers,
            ConnectionMethod
        };

        private Settings()
        {
            TextReader file = null;

            try
            {
                file = new StreamReader(new FileStream("filesharing.cfg", FileMode.OpenOrCreate));

                string line;
                string[] tokens = new string[2];
                Setting name;

                while ((line = file.ReadLine()) != null)
                {
                    int i;

                    i = line.IndexOf(' ', 0);
                    tokens[0] = line.Substring(0, i);

                    if (line[i + 1] != '=')
                        throw new Exception("Settings File: Wrong syntax.");

                    tokens[1] = line.Substring(i + 3, line.Length - i - 3);

                    if (tokens[1].Length == 0)
                        throw new Exception("Settings File: Wrong syntax.");

                    switch (tokens[0])
                    {
                        case "DefaultAddress":
                            name = Setting.DefaultAddress;
                            break;
                        case "DefaultPort":
                            name = Setting.DefaultPort;
                            break;
                        case "IncomingPort":
                            name = Setting.IncomingPort;
                            break;
                        case "RandomizeIncomingPort":
                            name = Setting.RandomizeIncomingPort;
                            break;
                        case "IncomingDirectory":
                            name = Setting.IncomingDirectory;
                            break;
                        case "UserName":
                            name = Setting.UserName;
                            break;
                        case "KnowUsers":
                            name = Setting.KnownUsers;
                            break;
                        case "ConnectionMethod":
                            name = Setting.ConnectionMethod;
                            break;
                        default:
                            throw new Exception("Settings File: Setting not defined.");
                    }

                    variables.Add(name, tokens[1]);
                }
            }
            catch (FileNotFoundException e)
            {
                // TODO: Create a new config file
            }
            catch (Exception e)
            {
            }
            finally
            {
                if (file != null)
                    file.Close();
            }
        }

        public static Settings Instance()
        {
            if (singleton == null)
                singleton = new Settings();

            return singleton;
        }

        public void Save()
        {
            TextWriter file = null;

            try
            {
                file = new StreamWriter(new FileStream("filesharing.cfg", FileMode.Create));

                file.WriteLine("DefaultAddress = " + GetValue(Setting.DefaultAddress));
                file.WriteLine("DefaultPort = " + GetValue(Setting.DefaultPort));
                file.WriteLine("IncomingPort = " + GetValue(Setting.IncomingPort));
                file.WriteLine("RandomizeIncomingPort = " + GetValue(Setting.RandomizeIncomingPort));
                file.WriteLine("IncomingDirectory = " + GetValue(Setting.IncomingDirectory));
                file.WriteLine("UserName = " + GetValue(Setting.UserName));
                file.WriteLine("ConnectionMethod = " + GetValue(Setting.ConnectionMethod));

                if (GetValue(Setting.KnownUsers).Length > 0)
                    file.WriteLine("KnowUsers = " + GetValue(Setting.KnownUsers));
                
            }
            catch (Exception e)
            {
            }
            finally
            {
                if (file != null)
                    file.Close();
            }

        }

        public String GetValue(Setting key)
        {
            try
            {
                return variables[key];
            }
            catch (KeyNotFoundException e)
            {
                switch (key)
                {
                    case Setting.UserName:
                        return "Unknown User";
                    case Setting.IncomingDirectory:
                        return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Downloads";
                    default:
                        return "";
                }
            }
        }

        public void SetValue(Setting key, string value)
        {
            try
            {
                variables[key] = value;
            }
            catch (KeyNotFoundException e)
            {
                variables.Add(key, value);
            }
        }
    }
}
